package com.lhb.controller;

import com.lhb.pojo.BaseResp;
import com.lhb.pojo.Teacher;
import com.lhb.service.TeacherService;
import com.lhb.utils.UploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.10.10 下午 13:33
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Resource
    TeacherService teacherService;

    @Autowired
    UploadUtils uploadUtils;

    @RequiresPermissions(value = {"TEACHER_FINDALL"})
    @RequestMapping("/findAll")
    public BaseResp findAll() {
        return teacherService.findAll();
    }

    @RequestMapping(value = "/findAllPage", method = RequestMethod.GET)
    public BaseResp findAllPage(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        BaseResp all = teacherService.findAllPage(page, limit);
        return all;
    }

    @RequestMapping("del")
    public BaseResp delete(@RequestParam("id") Integer id) {
        int i = teacherService.del(id);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(1);
        baseResp.setMsg("删除成功");
        return baseResp;
    }

    //图片上传
    @RequestMapping("/upload")
    public BaseResp upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        String upload = uploadUtils.upload(multipartFile);
        return new BaseResp(0, "上传成功！", null, upload);
    }

    //新增
    @RequestMapping(value = "/insertTeacher", method = RequestMethod.POST)
    public BaseResp insertCourse(@RequestBody Teacher teacher) {
        return teacherService.insertTeacher(teacher);
    }

    //修改
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResp updateTeacher(@RequestBody Teacher teacher) {
        return teacherService.update(teacher);
    }

}
